//
//  JSTextFieldTableCell.m
//  NiubilityProject
//
//  Created by Apple on 2018/4/2.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSTextFieldTableCell.h"
#import "JSCellTextFieldAdapter.h"

@implementation JSTextFieldTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setFieldAdapter:(JSCellTextFieldAdapter *)fieldAdapter{
    _fieldAdapter = fieldAdapter;
    self.delegate = fieldAdapter.delegate;
    self.titleL.text = fieldAdapter.titleName;
    self.accessoryType = fieldAdapter.accessoryType;
    if (JSRegularIsUrl(fieldAdapter.iconLink)) {
        [self.iconImageView setImageURL:[NSURL URLWithString:fieldAdapter.iconLink]];
    }else if(fieldAdapter.iconLink.length){
        self.iconImageView.image = UIImageMake(fieldAdapter.iconLink);
    }
}
- (void)js_buildSubview{
    _iconImageView = [YYAnimatedImageView new];
    _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    _textField = [QMUITextField new];
    _textField.delegate = self;
    _titleL = [YYLabel new];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidChangeValue:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:_textField];
    configLabel(_titleL, kBodyFontSize, JS_colorWithHex(kBodyBlack), @"title");
    configTextField(_textField, kBodyFontSize, JS_colorWithHex(kBodyBlack), @"Placeholder");
    [self.contentView js_addSubviews:@[_iconImageView,_textField, _titleL]];
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kRealValue(kBodyMargin_10));
        make.centerY.equalTo(self.contentView);
    }];
    [_titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(kRealValue(kBodyMargin_10));
        make.centerY.equalTo(self.iconImageView);
    }];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(kRealValue(-kBodyMargin_10));
        make.height.equalTo(self.titleL).multipliedBy(1.5);
        make.left.equalTo(self.titleL.mas_right).offset(kRealValue(kBodyMargin_10));
    }];
}
- (void)textFieldDidChangeValue:(NSNotification *)notification
{
    UITextField *sender = (UITextField *)[notification object];
    if ([self.textFieldDelegate respondsToSelector:@selector(textFieldTextDidChange:textString:)]) {
        [self.textFieldDelegate textFieldTextDidChange:self textString:sender.text];
    }
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
